//NOTE: Rooms containing A and B are 1 and 2, respectively
var roomGraph = argument0;
var pathGraph = argument1;
var minArea = argument2;
var height = array_height_2d(roomGraph);
var width = array_length_2d(roomGraph, 0);
var roomSeeds = ds_list_create();
var rooms = ds_list_create();

//If we pass a list of seeds to this function, we dont have to iterate through again
var startSeed = 0;
var finishSeed = 0;

for (var iii = 0; iii < height; iii++)
{
    for (var jjj = 0; jjj < width; jjj++)
    {
        var pathValue = pathGraph[iii, jjj];
        var roomValue = roomGraph[iii, jjj];
        if (roomValue == 1)
        {
            var location = 0;
            location[0] = iii;
            location[1] = jjj;
            if (is_string(pathValue) && pathValue == 'A')
            {
                startSeed = location
            }
            else if (is_string(pathValue) && pathValue == 'B')
            {
                finishSeed = location;
            }
            else
            {
                ds_list_add(roomSeeds, location);
            }
        }
    }
}

ds_list_insert(roomSeeds, 0, finishSeed);
ds_list_insert(roomSeeds, 0, startSeed);

var numSeeds = ds_list_size(roomSeeds);

//TODO: change in mark seeds function
//update roomSeeds to roomIds
for (var iii = 0; iii < numSeeds; iii++)
{
    var tile = ds_list_find_value(roomSeeds, iii);
    roomGraph[@ tile[0], tile[1]] = iii + 1;    
}

for (var iii = 0; iii < numSeeds; iii++)
{
    //room ids tell us which tiles belong to which rooms
    var roomId = iii + 1;
    var seed = ds_list_find_value(roomSeeds, iii);
    //a list of room tiles for the current room
    var floorTiles = ds_list_create();
    var maxArea = irandom(10) + minArea;
    var lastArea = 0;
    var area = 1;
    var iterations = 0;
    
    //add seed tile to list
    roomGraph[@ seed[0], seed[1]] = roomId;
    ds_list_add(floorTiles, seed); 
    
    //Set the subgraph we will work on for each seed
    var NWBound = 0;
    if (seed[0] - 3 > -1)
    {
        NWBound[0] = seed[0] - 3;
    }
    else
    {
        NWBound[0] = 0;
    }
    
    if (seed[1] - 3 > -1)
    {
        NWBound[1] = seed[1] - 3;
    }
    else
    {
        NWBound[1] = 0;
    }
    
    var SEBound = 0;
    if (seed[0] + 3 < height)
    {
        SEBound[0] = seed[0] + 3;
    }
    else
    {
        SEBound[0] = height - 1;
    }
    
    if (seed[1] + 3 < width)
    {
        SEBound[1] = seed[1] + 3;
    }
    else
    {
        SEBound[1] = width - 1;
    }
    
    //The actual corners of the connected component
    var NWCorner = 0;
    NWCorner[0] = seed[0];
    NWCorner[1] = seed[1];
    var SECorner = 0;
    SECorner[0] = seed[0];
    SECorner[1] = seed[1];   
    
    while (area < maxArea && lastArea != area)
    {
        lastArea = area;  
        var lastNWCorner = 0;
        lastNWCorner[0] = NWCorner[1];
        lastNWCorner[1] = NWCorner[1];
        var lastSECorner = 0;
        lastSECorner[0] = SECorner[0];
        lastSECorner[1] = SECorner[1];
    
        iterations++;
        var dir = iterations % 2;// N&S=0; E&W=1
        
        //North and south expansion
        if(dir == 0)
        {
            //Expand North and south with path overtake s.e.
            for (var yyy = NWBound[0]; yyy <= SEBound[0] - 3 /*for s.e. height*/; yyy++)
            {
                for (var xxx = NWBound[1] + 2; xxx <= SEBound[1] - 2; xxx++)
                {
                    if (area < maxArea && 
                        roomGraph[yyy + 3, xxx] == roomId && roomGraph[yyy + 2, xxx] == 0 && isPathTile(pathGraph, yyy + 2, xxx) &&
                        (roomGraph[yyy + 1, xxx] == roomId || roomGraph[yyy + 1, xxx] == 0) &&
                        (roomGraph[yyy + 2, xxx - 1] == roomId || roomGraph[yyy + 2, xxx - 1] == 0) &&
                        (roomGraph[yyy + 2, xxx + 1] == roomId || roomGraph[yyy + 2, xxx + 1] == 0))
                    {
                        var northFloor = 0;
                        northFloor[0] = yyy + 2;
                        northFloor[1] = xxx;  
                             
                        roomGraph[@ northFloor[0], northFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, northFloor); //Add tile to list for room module
                        area++;
                        
                        //Set new corners of the connected component
                        if (northFloor[0] < NWCorner[0])
                        {
                            NWCorner[0] = northFloor[0];
                        }
                        if (northFloor[1] < NWCorner[1])
                        {
                            //should not happen but put it here anyways
                            NWCorner[1] = northFloor[1];
                        }
                    }
                    if (area < maxArea && 
                        roomGraph[yyy, xxx] == roomId && roomGraph[yyy + 1, xxx] == 0 && isPathTile(pathGraph, yyy + 1, xxx) &&
                        (roomGraph[yyy + 2, xxx] == roomId || roomGraph[yyy + 2, xxx] == 0) &&
                        (roomGraph[yyy + 1, xxx - 1] == roomId || roomGraph[yyy + 1, xxx - 1] == 0) &&
                        (roomGraph[yyy + 1, xxx + 1] == roomId || roomGraph[yyy + 1, xxx + 1] == 0))
                    {
                        var southFloor = 0;
                        southFloor[0] = yyy + 1;
                        southFloor[1] = xxx;
                        
                        roomGraph[@  southFloor[0], southFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, southFloor);
                        area++;            
                        
                        //Set new corners of the connected component
                        if (southFloor[0] > SECorner[0])
                        {
                            SECorner[0] = southFloor[0];
                        }
                        if (southFloor[1] > SECorner[1])
                        {
                            SECorner[1] = southFloor[1];
                        }
                    }
                }
            }
            
            //Expand North and South with Normal Room Expansion s.e.
            for (var yyy = NWBound[0]; yyy <= SEBound[0] - 3 /*for s.e. height*/; yyy++)
            {
                for (var xxx = NWBound[1] + 2; xxx <= SEBound[1] - 2; xxx++)
                {
                    //Northern Expansion
                    if (area < maxArea &&
                        (roomGraph[yyy, xxx] == 0 && !isPathTile(pathGraph, yyy, xxx) || roomGraph[yyy, xxx] == roomId) && 
                        (roomGraph[yyy + 1, xxx] == 0 && !isPathTile(pathGraph, yyy + 1, xxx) || roomGraph[yyy + 1, xxx] == roomId) && 
                         roomGraph[yyy + 2, xxx] == 0 &&
                         roomGraph[yyy + 3, xxx] == roomId &&
                        (roomGraph[yyy + 2, xxx - 1] == 0 && !isPathTile(pathGraph, yyy + 2, xxx - 1) || roomGraph[yyy + 2, xxx - 1] == roomId) &&
                        (roomGraph[yyy + 2, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 1) || roomGraph[yyy + 2, xxx + 1] == roomId) && 
                        (roomGraph[yyy + 2, xxx - 2] == 0 && !isPathTile(pathGraph, yyy + 2, xxx - 2) || roomGraph[yyy + 2, xxx - 2] == roomId) &&
                        (roomGraph[yyy + 2, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 2) || roomGraph[yyy + 2, xxx + 2] == roomId) &&
                        (roomGraph[yyy + 1, xxx - 1] == 0 && !isPathTile(pathGraph, yyy + 1, xxx - 1) || roomGraph[yyy + 1, xxx - 1] == roomId) && 
                        (roomGraph[yyy + 1, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 1) || roomGraph[yyy + 1, xxx + 1] == roomId) &&
                        (roomGraph[yyy + 1, xxx - 2] == 0 && !isPathTile(pathGraph, yyy + 1, xxx - 2) || roomGraph[yyy + 1, xxx - 2] == roomId) && 
                        (roomGraph[yyy + 1, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 2) || roomGraph[yyy + 1, xxx + 2] == roomId) && 
                        (roomGraph[yyy, xxx - 1] == 0 && !isPathTile(pathGraph, yyy, xxx - 1) == 0 || roomGraph[yyy, xxx - 1] == roomId) &&
                        (roomGraph[yyy, xxx + 1] == 0 && !isPathTile(pathGraph, yyy, xxx + 1) || roomGraph[yyy, xxx + 1] == roomId) && 
                        (roomGraph[yyy, xxx - 2] == 0 && !isPathTile(pathGraph, yyy, xxx - 2) || roomGraph[yyy, xxx - 2] == roomId) && 
                        (roomGraph[yyy, xxx + 2] == 0 && !isPathTile(pathGraph, yyy, xxx + 2) || roomGraph[yyy, xxx + 2] == roomId) ) 
                    {
                        var northFloor = 0;
                        northFloor[0] = yyy + 2;
                        northFloor[1] = xxx;
                        
                        roomGraph[@ northFloor[0], northFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, northFloor); //Add tile to list for room module
                        area++;
                        
                        //Set new corners of the connected component
                        if (northFloor[0] < NWCorner[0])
                        {
                            NWCorner[0] = northFloor[0];
                        }
                        if (northFloor[1] < NWCorner[1])
                        {
                            //should not happen but put it here anyways
                            NWCorner[1] = northFloor[1];
                        }
                    }
                    //Southern Expansion
                    if (area < maxArea && 
                        (roomGraph[yyy, xxx] == roomId &&
                         roomGraph[yyy + 1, xxx] == 0 &&
                         (roomGraph[yyy + 2, xxx] == 0 && !isPathTile(pathGraph, yyy + 2, xxx) || roomGraph[yyy + 2, xxx] == roomId) &&
                         (roomGraph[yyy + 3, xxx] == 0 && !isPathTile(pathGraph, yyy + 3, xxx) || roomGraph[yyy + 3, xxx] == roomId) && 
                         (roomGraph[yyy + 1, xxx - 1] == 0 && !isPathTile(pathGraph, yyy + 1, xxx - 1) || roomGraph[yyy + 1, xxx - 1] == roomId) &&
                         (roomGraph[yyy + 1, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 1) || roomGraph[yyy + 1, xxx + 1] == roomId) &&
                         (roomGraph[yyy + 1, xxx - 2] == 0 && !isPathTile(pathGraph, yyy + 1, xxx - 2) || roomGraph[yyy + 1, xxx - 2] == roomId) &&
                         (roomGraph[yyy + 1, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 2) || roomGraph[yyy + 2, xxx + 2] == roomId) &&                         
                         (roomGraph[yyy + 2, xxx - 1] == 0 && !isPathTile(pathGraph, yyy + 2, xxx - 1) || roomGraph[yyy + 2, xxx - 1] == roomId) && 
                         (roomGraph[yyy + 2, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 1) || roomGraph[yyy + 2, xxx + 1] == roomId) &&
                         (roomGraph[yyy + 2, xxx - 2] == 0 && !isPathTile(pathGraph, yyy + 2, xxx - 2) || roomGraph[yyy + 2, xxx - 2] == roomId) && 
                         (roomGraph[yyy + 2, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 2) || roomGraph[yyy + 2, xxx + 2] == roomId) && 
                         (roomGraph[yyy + 3, xxx - 1] == 0 && !isPathTile(pathGraph, yyy + 3, xxx - 1) || roomGraph[yyy + 3, xxx - 1] == roomId) &&
                         (roomGraph[yyy + 3, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 3, xxx + 1) || roomGraph[yyy + 3, xxx + 1] == roomId) && 
                         (roomGraph[yyy + 3, xxx - 2] == 0 && !isPathTile(pathGraph, yyy + 3, xxx - 2) || roomGraph[yyy + 3, xxx - 2] == roomId) && 
                         (roomGraph[yyy + 3, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 3, xxx + 2) || roomGraph[yyy + 3, xxx + 2] == roomId)))
                    {
                        var southFloor = 0;
                        southFloor[0] = yyy + 1;
                        southFloor[1] = xxx;
                                                
                        roomGraph[@  southFloor[0], southFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, southFloor);
                        area++;         
        
                        //Set new corners of the connected component
                        if (southFloor[0] > SECorner[0])
                        {
                            SECorner[0] = southFloor[0];
                        }
                        if (southFloor[1] > SECorner[1])
                        {
                            SECorner[1] = southFloor[1];
                        }
                    }
                }
            }
        }
        else if (dir == 1)
        {
            //Expand West and East with path overtake s.e.
            for (var yyy = NWBound[0] + 2; yyy <= SEBound[0] - 2; yyy++)
            {
                for (var xxx = NWBound[1]; xxx <= SEBound[1] - 3 /*For S.E. width*/; xxx++)
                {
                    if (area < maxArea && 
                        roomGraph[yyy, xxx + 3] == roomId && roomGraph[yyy, xxx + 2] == 0 && isPathTile(pathGraph, yyy, xxx + 2) &&
                        (roomGraph[yyy, xxx + 1] == roomId || roomGraph[yyy, xxx + 1] == 0) &&
                        (roomGraph[yyy - 1, xxx + 2] == roomId || roomGraph[yyy - 1, xxx + 2] == 0) &&
                        (roomGraph[yyy + 1, xxx + 2] == roomId || roomGraph[yyy + 1, xxx + 2] == 0))
                    {
                        var westFloor = 0;
                        westFloor[0] = yyy;
                        westFloor[1] = xxx + 2;
                                                    
                        roomGraph[@ westFloor[0], westFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, westFloor);                            
                        area++;
                        
                        //Set new corners of the connected component
                        if (westFloor[0] < NWCorner[0])
                        {
                            NWCorner[0] = westFloor[0];
                        }
                        if (westFloor[1] < NWCorner[1])
                        {
                            NWCorner[1] = westFloor[1];
                        }
                    }
                    if (area < maxArea && 
                        roomGraph[yyy, xxx] == roomId && roomGraph[yyy, xxx + 1] == 0 && isPathTile(pathGraph, yyy, xxx + 1) &&
                        (roomGraph[yyy, xxx + 2] == roomId || roomGraph[yyy, xxx + 2] == 0) &&
                        (roomGraph[yyy - 1, xxx + 1] == roomId || roomGraph[yyy - 1, xxx + 1] == 0) &&
                        (roomGraph[yyy + 1, xxx + 1] == roomId || roomGraph[yyy + 1, xxx + 1] == 0))
                    {
                        var eastFloor = 0;
                        eastFloor[0] = yyy;
                        eastFloor[1] = xxx + 1;    
                        
                        roomGraph[@ eastFloor[0], eastFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, eastFloor);
                        area++;

                        //Set new corners of the connected component
                        if (eastFloor[0] > SECorner[0])
                        {
                            SECorner[0] = eastFloor[0];
                        }
                        if (eastFloor[1] > SECorner[1])
                        {
                            SECorner[1] = eastFloor[1];
                        }
                    }                    
                }
            }
            
            
            //Expand East and West with normal s.e.
            for (var yyy = NWBound[0] + 2; yyy <= SEBound[0] - 2; yyy++)
            {
                for (var xxx = NWBound[1]; xxx <= SEBound[1] - 3 /*For S.E. width*/; xxx++)
                {
                    //Western Expansion
                    if (area < maxArea &&
                        ((roomGraph[yyy, xxx] == 0 && !isPathTile(pathGraph, yyy, xxx) || roomGraph[yyy, xxx] == roomId) && 
                        (roomGraph[yyy, xxx + 1] == 0 && !isPathTile(pathGraph, yyy, xxx + 1) || roomGraph[yyy, xxx + 1] == roomId) && 
                        roomGraph[yyy, xxx + 2] == 0 &&
                        roomGraph[yyy, xxx + 3] == roomId && 
                        (roomGraph[yyy - 1, xxx + 2] == 0 && !isPathTile(pathGraph, yyy - 1, xxx + 2) || roomGraph[yyy - 1, xxx + 2] == roomId) &&
                        (roomGraph[yyy + 1, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 2) || roomGraph[yyy + 1, xxx + 2] == roomId) &&
                        (roomGraph[yyy - 2, xxx + 2] == 0 && !isPathTile(pathGraph, yyy - 2, xxx + 2) || roomGraph[yyy - 2, xxx + 2] == roomId) &&
                        (roomGraph[yyy + 2, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 2) || roomGraph[yyy + 2, xxx + 2] == roomId) &&
                        (roomGraph[yyy - 1, xxx + 1] == 0 && !isPathTile(pathGraph, yyy - 1, xxx + 1) || roomGraph[yyy - 1, xxx + 1] == roomId) && 
                        (roomGraph[yyy + 1, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 1) || roomGraph[yyy + 1, xxx + 1] == roomId) &&
                        (roomGraph[yyy - 2, xxx + 1] == 0 && !isPathTile(pathGraph, yyy - 2, xxx + 1) || roomGraph[yyy - 2, xxx + 1] == roomId) && 
                        (roomGraph[yyy + 2, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 1) || roomGraph[yyy + 2, xxx + 1] == roomId) && 
                        (roomGraph[yyy - 1, xxx] == 0 && !isPathTile(pathGraph, yyy - 1, xxx) || roomGraph[yyy - 1, xxx] == roomId) &&
                        (roomGraph[yyy + 1, xxx] == 0 && !isPathTile(pathGraph, yyy + 1, xxx) || roomGraph[yyy + 1, xxx] == roomId) && 
                        (roomGraph[yyy - 2, xxx] == 0 && !isPathTile(pathGraph, yyy - 2, xxx) || roomGraph[yyy - 2, xxx] == roomId) && 
                        (roomGraph[yyy + 2, xxx] == 0 && !isPathTile(pathGraph, yyy + 2, xxx) || roomGraph[yyy + 2, xxx] == roomId) ))
                    {
                        var westFloor = 0;
                        westFloor[0] = yyy;
                        westFloor[1] = xxx + 2;
                                       
                        roomGraph[@ westFloor[0], westFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, westFloor);                            
                        area++;
                        
                        //Set new corners of the connected component
                        if (westFloor[0] < NWCorner[0])
                        {
                            NWCorner[0] = westFloor[0];
                        }
                        if (westFloor[1] < NWCorner[1])
                        {
                            NWCorner[1] = westFloor[1];
                        }
                    }
                    //Eastern Expansion
                    if (area < maxArea &&
                         (roomGraph[yyy, xxx] == roomId && 
                         roomGraph[yyy, xxx + 1] == 0 && 
                         (roomGraph[yyy, xxx + 2] == 0 && !isPathTile(pathGraph, yyy, xxx + 2) || roomGraph[yyy, xxx + 2] == roomId) &&
                         (roomGraph[yyy, xxx + 3] == 0 && !isPathTile(pathGraph, yyy, xxx + 3) || roomGraph[yyy, xxx + 3] == roomId) && 
                         (roomGraph[yyy - 1, xxx + 1] == 0 && !isPathTile(pathGraph, yyy - 1, xxx + 1) || roomGraph[yyy - 1, xxx + 1] == roomId) &&
                         (roomGraph[yyy + 1, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 1) || roomGraph[yyy + 1, xxx + 1] == roomId) &&
                         (roomGraph[yyy - 2, xxx + 1] == 0 && !isPathTile(pathGraph, yyy - 2, xxx + 1) || roomGraph[yyy - 2, xxx + 2] == roomId) &&
                         (roomGraph[yyy + 2, xxx + 1] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 1) || roomGraph[yyy + 2, xxx + 2] == roomId) &&
                         (roomGraph[yyy - 1, xxx + 2] == 0 && !isPathTile(pathGraph, yyy - 1, xxx + 2) || roomGraph[yyy - 1, xxx + 2] == roomId) && 
                         (roomGraph[yyy + 1, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 2) || roomGraph[yyy + 1, xxx + 2] == roomId) &&
                         (roomGraph[yyy - 2, xxx + 2] == 0 && !isPathTile(pathGraph, yyy - 2, xxx + 2) || roomGraph[yyy - 2, xxx + 2] == roomId) && 
                         (roomGraph[yyy + 2, xxx + 2] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 2) || roomGraph[yyy + 2, xxx + 2] == roomId) && 
                         (roomGraph[yyy - 1, xxx + 3] == 0 && !isPathTile(pathGraph, yyy - 1, xxx + 3) || roomGraph[yyy - 1, xxx + 3] == roomId) &&
                         (roomGraph[yyy + 1, xxx + 3] == 0 && !isPathTile(pathGraph, yyy + 1, xxx + 3) || roomGraph[yyy + 1, xxx + 3] == roomId) && 
                         (roomGraph[yyy - 2, xxx + 3] == 0 && !isPathTile(pathGraph, yyy - 2, xxx + 3) || roomGraph[yyy - 2, xxx + 3] == roomId) && 
                         (roomGraph[yyy + 2, xxx + 3] == 0 && !isPathTile(pathGraph, yyy + 2, xxx + 3) || roomGraph[yyy + 2, xxx + 3] == roomId) ))
                    {
                        var eastFloor = 0;
                        eastFloor[0] = yyy;
                        eastFloor[1] = xxx + 1;    
                        
                        roomGraph[@ eastFloor[0], eastFloor[1]] = roomId; //Set tile to room component
                        ds_list_add(floorTiles, eastFloor);
                        area++;

                        //Set new corners of the connected component
                        if (eastFloor[0] > SECorner[0])
                        {
                            SECorner[0] = eastFloor[0];
                        }
                        if (eastFloor[1] > SECorner[1])
                        {
                            SECorner[1] = eastFloor[1];
                        }
                    }
                }
            }
        }
        
        //Update boundaries
        //If we created a new corner
        if (NWCorner[0] != lastNWCorner[0])
        {
            if (NWCorner[0] - 3 > -1)
            {
                NWBound[0] = NWCorner[0] - 3;
            }
            else
            {
                NWBound[0] = 0;
            }
        }
        
        if (NWCorner[1] != lastNWCorner[1])
        {
            if (NWCorner[1] - 3 > -1)
            {
                NWBound[1] = NWCorner[1] - 3; 
            }
            else
            {
                NWBound[1] = 0;
            }
        }
        
        if (SECorner[0] != lastSECorner[1])
        {
            if(SECorner[0] + 3 < height)
            {
                SEBound[0] = SECorner[0] + 3;
            }
            else
            {
                SEBound[0] = height - 1;
            }
        }
        
        if (SECorner[1] != lastSECorner[1])
        {
            if (SECorner[1] + 3 < width)
            {
                SEBound[1] = SECorner[1] + 3;
            }
            else
            {
                SEBound[1] = width - 1;            
            }
        }         
    }    
    
    //Fill out rooms (Match three adjacencies -> Fill)
    
    var filled = false;
    do
    {
        filled = false;
        
        for (var yyy = NWCorner[0]; yyy <= SECorner[0]; yyy++)
        {
            for (var xxx = NWCorner[1]; xxx <= SECorner[1]; xxx++)
            {
                if (roomGraph[yyy, xxx] == 0)
                {
                    var adjacencies = 0;
                    if (roomGraph[yyy, xxx + 1] == roomId)
                    {
                        adjacencies++;
                    }
                    if (roomGraph[yyy - 1, xxx] == roomId)
                    {
                        adjacencies++;
                    } 
                    if (roomGraph[yyy, xxx - 1] == roomId)
                    {
                        adjacencies++;
                    }
                    if (roomGraph[yyy + 1, xxx] == roomId)
                    {
                        adjacencies++;
                    }
                    if (adjacencies >= 3 && xxx + 2 < width && yyy - 2 > -1 && xxx - 2 > -1 && yyy + 2 < height &&
                        (roomGraph[yyy, xxx + 2] == roomId || roomGraph[yyy, xxx + 2] == 0 && (!roomGraph[yyy, xxx + 1] && pathGraph[yyy, xxx + 2] == 0 || roomGraph[yyy, xxx + 1])) &&
                        (roomGraph[yyy - 2, xxx] == roomId || roomGraph[yyy - 2, xxx] == 0 && (!roomGraph[yyy - 1, xxx] && pathGraph[yyy - 2, xxx] == 0 || roomGraph[yyy - 1, xxx])) &&
                        (roomGraph[yyy, xxx - 2] == roomId || roomGraph[yyy, xxx - 2] == 0 && (!roomGraph[yyy, xxx - 1] && pathGraph[yyy, xxx - 2] == 0 || roomGraph[yyy, xxx - 1])) &&
                        (roomGraph[yyy + 2, xxx] == roomId || roomGraph[yyy + 2, xxx] == 0 && (!roomGraph[yyy + 1, xxx] && pathGraph[yyy + 2, xxx] == 0 || roomGraph[yyy + 1, xxx])))
                    {   
                        var filledFloor = 0;
                        filledFloor[0] = yyy;
                        filledFloor[1] = xxx;
                        roomGraph[@yyy, xxx] = roomId;
                        ds_list_add(floorTiles, filledFloor);                            
                        area++;
                        filled = true;
                    }       
                }         
            }
        }
        
    } until (!filled);
        
    //Erase room if not big enough
    if (area < minArea && roomId != 1 && roomId != 2)
    {
        for(var jjj = 0; jjj < ds_list_size(floorTiles); jjj++)
        {
            var tile = ds_list_find_value(floorTiles, jjj);
            roomGraph[@tile[0], tile[1]] = 0;
        }
    }
    
    //Create a room module  
    else 
    {           
        ds_list_add(rooms, roomFactory(NWCorner, floorTiles, roomGraph, pathGraph));
    }
    
    ds_list_destroy(floorTiles);
}

ds_list_destroy(roomSeeds);

roomsArray = 0;
for (var i = 0; i < ds_list_size(rooms); i++)
{
    roomsArray[i] = ds_list_find_value(rooms, i);
}

ds_list_destroy(rooms);

return roomsArray;

